const Storage = require("../../utils/storage.js");
const session = require("../../utils/session.js");
const utils = require("../../utils/utils.js");
const toast = require("../../utils/toasts.js");
const com = require("../../utils/commonRequest.js");
const biz = require("../../utils/bizUtils.js");
const cart = require("../../utils/cart.js");
import {
  req,
} from "../../utils/api.js";
const wxm = require("../../utils/wechat_modules.js");

const api = require("../../utils/api.js");
const app = getApp();
const globalData = app.globalData;

Page({
  data: {
    favList: [],
    viewList: [],
    isShowLoginModal: false,
    isShowBindPhoneModal: true,
    banners: ['https://img.lingxing.site/jx/small_banner_jx1.png'],
    banners4: ['https://img.lingxing.site/jx/small_banner_jx4.png'],
    menus: [{
      'title': '顺序练习',
      'subTitle': 'IN ORDER',
      type: 'order',
      cateid: 1
    }, {
      'title': '随机练习',
      'subTitle': 'RANDOM',
      type: 'ran',
      cateid: 1
    }, {
      'title': '章节练习',
      'subTitle': 'CHAPTER',
      type: 'chap',
      cateid: 1
    }, {
      'title': '收藏题目',
      'subTitle': 'FAV',
      type: 'fav',
      cateid: 1
    }, {
      'title': '错题练习',
      'subTitle': 'WRONG',
      type: 'wrong',
      cateid: 1
    }, {
      'title': '模拟考试',
      'subTitle': 'PRACTICE',
      type: 'test',
      cateid: 1
    }],
    menus4: [{
      'title': '顺序练习',
      'subTitle': 'IN ORDER',
      type: 'order',
      cateid: 4
    }, {
      'title': '随机练习',
      'subTitle': 'RANDOM',
      type: 'ran',
      cateid: 4
    }, {
      'title': '章节练习',
      'subTitle': 'CHAPTER',
      type: 'chap',
      cateid: 4
    }, {
      'title': '收藏题目',
      'subTitle': 'FAV',
      type: 'fav',
      cateid: 4
    }, {
      'title': '错题练习',
      'subTitle': 'WRONG',
      type: 'wrong',
      cateid: 4
    }, {
      'title': '模拟考试',
      'subTitle': 'PRACTICE',
      type: 'test',
      cateid: 4
    }],
    applogo: globalData.applogo,
    isIphoneX: globalData.isIphoneX,
    tabIndex1: 0,
    tabs: [{
        "selected": true,
        "text": "科目一",
      },
      {
        "text": "科目二",
      },
      {
        "text": "科目三",
      },
      {
        "text": "科目四",
      }
    ],
  },
  onLoad: function(options) {
    // toast.showLoading();
    if (options.from) {
      this.setData({
        from:options.from
      })
    }

    session.login(this.refreshMe);
    this.selectComponent('#tab').changeTab(0);
  },
  refreshMe: function(usr) {
    this.setData({
      user: usr
    });
  },
  onShow: function() {},
  refresh: function() {},
  // 获取当前用户信息
  getUserInfo: function() {
    session.getUser(this);
  },
  showLoginModal: function() {
    this.setData({
      isShowLoginModal: true
    });
  },
  closeLogin: function() {
    session.getUser(this);
    this.setData({
      isShowLoginModal: false
    });
  },
  onShareAppMessage: function(res) {
    var t = '2k学车，免费在线学习科目一、科目二、科目三、科目四';
    var url = '/pages/questionList/questionList?1=1';
    return wxm.baseShare(this,t,url,'');
  },
  hideShare: function() {
    this.setData({
      showShareImg: false
    });
  },
  pushFormSubmit: function(e) {
    console.log(e);
    console.log("formId:" + e.detail.formId);
    com.saveFormId(e);
  },
  banners: function() {
    // com.banners(this);
  },
  showFavXcx: function() {
    var that = this;
    var showFavXcx = wx.getStorageSync("showFavXcx");
    if (showFavXcx && showFavXcx.value) {
      return;
    }

    Storage.set("showFavXcx", true, 24 * 60 * 60 * 5);

    this.setData({
      showFavXcx: true
    });

    setTimeout(function() {
      that.setData({
        showFavXcx: false
      })
    }, 3000);
  },
  gotoSellerList: function() {
    wx.navigateTo({
      url: '/pages/sellerList/sellerList',
    })
  },
  gotoNextPage: function(e) {
    var cateid = e.currentTarget.dataset.cateid;
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/questionDetail/questionDetail?type=' + type + '&cateid=' + cateid,
    })
  },
  switchTab1: function(e) {
    var tabIndex1 = e.detail.index;
    this.setData({
      tabIndex1
    })
    var that = this;
    if (tabIndex1 == 1) {
      this.getVideos('JX2');
    } else if (tabIndex1 == 2) {
      this.getVideos('JX3');
    }
  },
  getVideos: function(type) {
    var that = this;
    var param = {};
    param.type = type;
    param.pageNo = 1;
    param.pageSize = 100;
    param.orderRule = 'DEFAULT';

    if (type == 'JX2' && this.data.jx2List) {
      return;
    } else if (type == 'JX3' && this.data.jx3List) {
      return;
    }

    toast.showLoading();

    var api = "/jxq/queryVideo";
    req(api, param).then(res => {
      // console.log(res);
      if (res.success) {
        var list = res.result.list;
        if (type == 'JX2') {
          that.setData({
            jx2List: list
          })
        } else if (type == 'JX3') {
          that.setData({
            jx3List: list
          })
        }
      } else {}
    });
  },
  play: function(e) {
    var index = e.currentTarget.dataset.index;
    var type = e.currentTarget.dataset.type;

    var list;
    if (type == 'JX2') {
      list = this.data.jx2List;
    } else if (type == 'JX3') {
      list = this.data.jx3List;
    }

    for (var i = 0; i < list.length; i++) {
      var item = list[i];
      if (item.play) {
        this.pause(i, type);
      }
    }

    list[index].play = true;
    if (type == 'JX2') {
      this.setData({
        jx2List: list
      })
    } else if (type == 'JX3') {
      this.setData({
        jx3List: list
      })
    }

    this.playVideo(index, type);
  },
  pause: function(index, type) {
    var vc = wx.createVideoContext("video" + index + type, this);
    if (vc) {
      vc.pause();
    }
  },
  playVideo: function(index, type) {
    var vc = wx.createVideoContext("video" + index + type, this);
    if (vc) {
      vc.play();
    }
  },
  gotoHome: function () {
    biz.gotoPage('/pages/index/index');
  },
});